//
//  mvp.m
//  EFExtend
//
//  Created by mac on 15/3/19.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "mvp.h"
#import "UserPresenter.h"
#import "CKBPresenter.h"

@interface TESTMODEL : NSObject
@property (nonatomic,strong) NSNumber *errno__alias;
@property (nonatomic,copy) NSString *errde;

@end

@implementation TESTMODEL



@end


@interface mvp ()<PresenterCallBackProtocol>
@property (nonatomic,strong) CKBPresenter *ckbPresenter;
@end

@implementation mvp
{
    UserPresenter *userp;
}
- (void)dealloc
{
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    [self.customNavRightBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavRightBtn setFrame:CGRectMakeBound(32, 32)];
    
    userp = [[UserPresenter alloc]init];
    userp.delegate = self;
    
    self.ckbPresenter = [[CKBPresenter alloc]init];
    self.ckbPresenter.delegate = self;
    
    self.view.backgroundColor = [UIColor md_grey_100];
    
    [self initialized];
    
    [self testJSON2Model];
//    self.currentTerminal.Token = @"SParS8pcf0waT6QtSJSkCr9WZoY2Bpb1u0Af9jhhCOUiREYcML2kdvSUkdVEs2AEvGqZhJ0vnHYEb2";
//    self.currentTerminal.TermianlNumber = @"460040340509998";
    //获取位置
    [self.ckbPresenter doStore:[CKBPresenter GetLastGpsItemIO_terminalToken] attributes:@"SParS8pcf0waT6QtSJSkCr9WZoY2Bpb1u0Af9jhhCOUiREYcML2kdvSUkdVEs2AEvGqZhJ0vnHYEb2"];
    //获取设防状态
    [self.ckbPresenter doStore:[CKBPresenter QueryAreaSettingIO_terminalKey] attributes:@"460040340509998"];
    
    [self getLocation];
    [self getStatus];
}

- (void)getLocation{
    [self.ckbPresenter doSomething:self.ckbPresenter.GetLastGpsItem attributes:nil];
}

- (void)getStatus{
    [self.ckbPresenter doSomething:self.ckbPresenter.QueryAreaSetting attributes:nil];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initialized{
    HMUITextField *un = [HMUITextField spawn];
    un.text = @"18606066623";
    un.backgroundColor = [UIColor whiteColor];
    un.keyboardAppearance = UIKeyboardAppearanceLight;
    [self.view addSubview:un];
    un.tagString = @"IOUsername";
    [un mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(350);
        make.left.mas_equalTo(self.view.mas_left).offset(30);
        make.height.mas_equalTo(40);
        make.right.mas_equalTo(self.view.mas_right).offset(-30);
    }];
    
    [userp bindInputOutput:un forAttribute:un.tagString];
    
    HMUITextField *pw = [HMUITextField spawn];
//    pw.text = @"0000";
    pw.fieldStyle = TextFieldStyleTopTips;
    pw.bottomLineColor = [UIColor md_orange_400];
    pw.topTipsText = @"cao ni mei";
    pw.placeholder  = @"输入密码";
    pw.backgroundColor = [UIColor whiteColor];
    pw.shouldHideIfReturn = YES;
    [self.view addSubview:pw];
    pw.tagString = @"IOPassword";
    [pw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(un.mas_bottom).offset(30);
        make.left.mas_equalTo(self.view.mas_left).offset(30);
        make.height.mas_equalTo(40);
        make.right.mas_equalTo(self.view.mas_right).offset(-30);
    }];
    [userp bindInputOutput:pw forAttribute:pw.tagString];
    un.nextChain = pw;
}

- (void)testJSON2Model{
    NSDictionary *dic = @{@"errno":@(0),@"errde":@"123"};
    TESTMODEL *model = [TESTMODEL objectFromDictionary:dic];
    if (model) {
        
    }
}

- (void)presenter:(Presenter*)presenter doSome:(NSString *)something bean:(id)bean state:(PresenterProcess)state error:(NSError *)error{
    INFO(@"back to Ctroller",something);
}

ON_TextField(signal){
    HMUITextField *ff = signal.source;
    if ([signal is:[HMUITextField WILL_ACTIVE]]) {
        [HMUIKeyboard sharedInstance].accessor = ff;
        [HMUIKeyboard sharedInstance].visabler = ff;
    }
    
    if (ff.text.length > 10&&ff.text.length<20) {
        ff.topTipsText = @"要命输入那么长";
        ff.topTipsColor = [UIColor md_red_400];
    }else if (ff.text.length > 20) {
        ff.topTipsText = @"容不下你了";
    }else{
        ff.topTipsText = ff.placeholder;
        ff.topTipsColor = [UIColor blueColor];
    }
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            [userp doSomething:@"Login" attributes:nil];
        }
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (touches) {
        
    }
}
ON_WebAPI(dlg){
    if (dlg.sending) {
        
        
    }else if (dlg.succeed){
//        NSDictionary *dic = [dlg.output objectForKey:[HMWebAPI params]];
        
        
    }else if (dlg.failed){
        if (dlg.timeout) {
            [self showFailureTip:@"操作失败" detail:@"链接超时" timeOut:3.f];
            return;
        }
        
        [self showFailureTip:@"网络链接失败" detail:@"数据不存在或网络错误" timeOut:3.f];
    }else if (dlg.cancelled){
        
    }
//#if (__ON__ == __HM_DEVELOPMENT__)
//    CC( [self.class description],dlg.responseData?dlg.responseData:dlg.errorDesc);
//#endif	// #if (__ON__ == __BEE_DEVELOPMENT__)
}
@end
